package demo.practice.leetcode_msjd;

import java.util.Stack;

public class Msjd0302 {
    public static void main(String[] args) {

        MinStack minStack= new MinStack();
        minStack.push(2);
        minStack.push(0);
        minStack.push(3);
        minStack.push(0);

        minStack.pop();
        minStack.pop();
        minStack.pop();

        System.out.println("chenwang");
    }

}


class MinStack {

    /** initialize your data structure here. */


    private Stack<Integer> data= new Stack<>();
    private Stack<Integer> min= new Stack<>();

    public MinStack() {

    }

    public void push(int x) {
       /* data.push(x);
        if(min.empty()||x<min.peek())
            min.push(x);
        else
            min.push(min.peek());*/

        data.push(x);
        if(min.empty()||x<=min.peek())  //这里注意，一定要把重复的最小值压入栈。不然会错位
            min.push(x);
    }

    public void pop() {
        //但为什么 直接 if (stack1.pop() == getMin())  可以？？？
        //Integer pop = data.pop();     错误，因为Integer   超过128后 会新建对象。。。
        int pop = data.pop();
        if(pop==min.peek())
            min.pop();

    }

    public int top() {
        return data.peek();
    }

    public int getMin() {
        return min.peek();
    }
}
